package com.yaozheng.vocationaltraining.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/9/3.
 */
public class TimeUtils {
    public static final SimpleDateFormat DEFAULT_SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 字符串转时间
     *
     * @param dateStr
     * @return
     */
    public static Date stringToDate(String dateStr) {
        return stringToDate(dateStr, DEFAULT_SIMPLE_DATE_FORMAT);
    }

    /**
     * 字符串转时间
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date stringToDate(String dateStr, SimpleDateFormat format) {
        try {
            if (dateStr == null || "".equals(dateStr)) {
                return null;
            }
            return format.parse(dateStr);
        } catch (ParseException e) {
            ErrorUtils.outErrorLog(e);
        }
        return null;
    }

    /**
     * 字符串转时间戳
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static long stringToTime(String dateStr, SimpleDateFormat format) {
        Date date = stringToDate(dateStr, format);
        if (date == null) {
            return 0;
        } else {
            return date.getTime();
        }
    }

    public static long stringToTime(String dateStr) {
        return stringToTime(dateStr, DEFAULT_SIMPLE_DATE_FORMAT);
    }
}
